#!/usr/bin/python
# -*- coding: utf-8 -*-
# Description: Description
# Create at 2008-11-23 下午11:39:52
# Author: MK2[fengmk2@gmail.com]
"""docstrings
"""

import unittest
import net4.util

class UtilTest(unittest.TestCase):
    
    def test_get_os_re(self):
        user_agents = (#windows ie
                       ('Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0)', 
                        {'os':'Windows NT 6.0',
                         'browser': 'MSIE 8.0',
                         'base': 'Mozilla/4.0'}),
                       ('Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.2)', 
                        {'os':'Windows NT 5.2',
                         'browser': 'MSIE 7.0',
                         'base': 'Mozilla/4.0'}),
                       ('Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)', 
                        {'os':'Windows NT 5.1',
                         'browser': 'MSIE 6.0',
                         'base': 'Mozilla/4.0'}),
                       ('Mozilla/4.0 (compatible; MSIE 5.0; Windows NT)', 
                        {'os':'Windows NT',
                         'browser': 'MSIE 5.0',
                         'base': 'Mozilla/4.0'}),
                        
                        #maxthon
                        ('Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; Maxthon; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022),gzip(gfe)',
                         {'os':'Windows NT 5.1',
                         'browser': 'MSIE 6.0',
                         'base': 'Mozilla/4.0'}),
                         
                         #windows firefox
                        (' Mozilla/5.0 (Windows; U; Windows NT 5.2) Gecko/2008070208 Firefox/3.0.1',
                         {'os': 'Windows NT 5.2',
                          'browser': 'Firefox/3.0.1',
                          'base': 'Mozilla/5.0'}),
                        ('Mozilla/5.0 (Windows; U; Windows NT 5.1) Gecko/20070309 Firefox/2.0.0.3',
                         {'os': 'Windows NT 5.1',
                          'browser': 'Firefox/2.0.0.3',
                          'base': 'Mozilla/5.0'}),
                         ('Mozilla/5.0 (Windows; U; Windows NT 5.1) Gecko/20070803 Firefox/1.5.0.12',
                          {'os': 'Windows NT 5.1',
                          'browser': 'Firefox/1.5.0.12',
                          'base': 'Mozilla/5.0'}),
                          
                          #ubuntu firefox
                        ('Mozilla/5.0 (X11; U; Linux i686; zh-CN; rv:1.9.0.3) Gecko/2008101315 Ubuntu/8.10 (intrepid) Firefox/3.0.3',
                         {'os': 'Linux i686',
                          'browser': 'Firefox/3.0.3',
                          'base': 'Mozilla/5.0'}),
                          
#                          #Opera
#                        ('Opera/9.27 (Windows NT 5.2; U; zh-cn)',
#                         {'os': 'Windows NT 5.2',
#                          'browser': 'Opera/9.27',
#                          'base': 'Opera/9.27'}),
#                         ('Opera/8.0 (Macintosh; PPC Mac OS X; U; en)',
#                         {'os': 'PPC Mac OS X',
#                          'browser': 'Opera/8.0',
#                          'base': 'Opera/8.0'}),
#                         ('Mozilla/5.0 (Macintosh; PPC Mac OS X; U; en) Opera 8.0',
#                          {'os': 'PPC Mac OS X',
#                          'browser': 'Opera 8.0',
#                          'base': 'Mozilla/5.0'}),
                          
                         # Safari
                         ('Mozilla/5.0 (Windows; U; Windows NT 5.2) AppleWebKit/525.13 (KHTML, like Gecko) Version/3.1 Safari/525.13',
                          {'os': 'Windows NT 5.2',
                          'browser': 'Safari/3.1',
                          'base': 'Mozilla/5.0'}),
                         ('Mozilla/5.0 (iPhone; U; CPU like Mac OS X) AppleWebKit/420.1 (KHTML, like Gecko) Version/3.0 Mobile/4A93 Safari/419.3',
                          {'os': 'CPU like Mac OS X',
                          'browser': 'Safari/3.0',
                          'base': 'Mozilla/5.0'}), 
                          
                         # Chrome
                         ('Mozilla/5.0 (Windows; U; Windows NT 5.2) AppleWebKit/525.13 (KHTML, like Gecko) Chrome/0.2.149.27 Safari/525.13',
                          {'os': 'Windows NT 5.2',
                          'browser': 'Chrome/0.2.149.27',
                          'base': 'Mozilla/5.0'}),
                       )
        for user_agent, want_r in user_agents:
            r = util.analysis_user_agent(user_agent)
            print r
            self.assertTrue(r)
            for key, val in want_r.iteritems():
                self.assertTrue(key in r)
                self.assertEqual(r[key], val)
            
if __name__ == '__main__':
    unittest.main()
